clear
clear all

********************************************************************************
//Comining raw data and averaging over partners
loc run = 0
if `run' == 1{
	
cd "C:\Users\...\Tariff Analysis"  //Home of Ddata from WITS
foreach num of numlist 1997/2020 {
	use "alltariffs_`num'.dta", clear
		collapse (mean) best_adv best_ave *_simpleaverage, by(exporteriso3 productcode year) fast
		rename exporteriso3 iso3code
	save  "$temp/tf_`num'.dta", replace
}

}

loc run = 0
if `run' == 1{

use "$temp/tf_1997.dta", clear
	foreach num of numlist 1997(1)2020 {
	append using "$temp/tf_`num'.dta", force
}
rename iso3code countrycode
compress
save "$data/tf_total_2024.dta", replace

}

********************************************************************************
//Tariffs for analysis
loc run = 1
if `run' == 1{
 
use "$data\tf_total_2024.dta", clear //Tariffs downloaded 2021
rename countrycode c
rename productcode h6
rename year y

*Tariff
*gen tf=.
*replace tf=pref_simpleaverage if pref_simpleaverage!=.
*replace tf=mfn_simpleaverage if tf==.
*Dropping duplicates
duplicates tag c h6 y, g(dup) 
tab dup /* 5.56% duplicates */
collapse (max) best_adv mfn_simpleaverage pref_simpleaverage, by(c h6 y) fast
*** HS consolidates
rename h6 hs
* Merging below the file with consolidated HS codes (variable h) and their mapping to ANY hs6 codes from any revision ----- the ultimate HS code that will be used is the variable h
merge m:1 hs using "$data/consolidation_1996_2012_data_convert2017.dta"
* HS6 codes that are not included in the consolidation dataset have not changed across revisions and therefore they remain themselves
replace h=hs if _m==1
drop if _m==2
drop _m
collapse (mean) best_adv mfn_simpleaverage pref_simpleaverage, by(c h y) fast
rename h h6
sort c h6 y

rename best_adv tf_best_adv
rename mfn_simpleaverage tf_mfn_simpleaverage 
rename pref_simpleaverage tf_pref_simpleaverage

*** Tariffs 
foreach var of varlist tf_best_adv tf_mfn_simpleaverage  tf_pref_simpleaverage {
				replace `var'=`var'/100
				//replace `var'=. if `var'>1
        }

keep c h6 y tf_best_adv tf_mfn_simpleaverage  tf_pref_simpleaverage
save  "$temp\tariff.dta", replace
}



********************************************************************************
//NTMs
loc run = 1
if `run' == 1{
use "$data\NTM_hs6_2010_2022_H4v12.dta", clear

*** HS consolidates
rename HSCode hs
* Merging below the file with consolidated HS codes (variable h) and their mapping to ANY hs6 codes from any revision ----- the ultimate HS code that will be used is the variable h
merge m:1 hs using "$data/consolidation_1996_2012_data_convert2017.dta"
* HS6 codes that are not included in the consolidation dataset have not changed across revisions and therefore they remain themselves
replace h=hs if _m==1
drop if _m==2
drop _m

rename Year y 
rename Reporter c
keep h c y NTMCode
duplicates drop

gen chapter = substr(NTMCode,1,1)
keep h c y chapter

*Dummies by NTM chapter
levelsof chapter, local(ntm)
foreach n in `ntm'{
    gen ntm_`n' = (chapter=="`n'")
}

drop chapter
egen ntm_DEF = rowmax(ntm_D ntm_E ntm_F)
egen ntm_EF = rowmax(ntm_E ntm_F)
egen ntm_ABCDEF = rowmax(ntm_A ntm_B ntm_C ntm_D ntm_E ntm_F)
egen ntm_ABC = rowmax(ntm_A ntm_B ntm_C)

compress

sort c h y 

collapse (max) ntm_*, by(y c h)

save "$temp\ntm_trains.dta", replace
	
}


